package com.sugon.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import com.sugon.dao.ModuleMaintainRecordDao;
import com.sugon.entity.ModuleMaintainPlanItemEntity;
import com.sugon.entity.ModuleMaintainRecordEntity;
import com.sugon.service.ModuleMaintainPlanItemService;
import com.sugon.service.ModuleMaintainRecordService;
import com.sugon.utils.MapUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 模具维护记录单Service实现类
 *
 * @author sugon
 * @date 2019-08-09 11:28:49
 */
@Service("moduleMaintainRecordService")
public class ModuleMaintainRecordServiceImpl implements ModuleMaintainRecordService {
    @Autowired
    private ModuleMaintainRecordDao moduleMaintainRecordDao;
    @Autowired
    private ModuleMaintainPlanItemService moduleMaintainPlanItemService;

    /**
     *
     * @param id 主键
     * @return
     */
    @Override
    public ModuleMaintainRecordEntity queryObject(String id) {
        return moduleMaintainRecordDao.queryObject(id);
    }

    /**
     *
     * @param map 参数
     * @return
     */
    @Override
    public List<ModuleMaintainRecordEntity> queryList(Map<String, Object> map) {
        return moduleMaintainRecordDao.queryList(map);
    }

    /**
     *
     * @param map 参数
     * @return
     */
    @Override
    public int queryTotal(Map<String, Object> map) {
        return moduleMaintainRecordDao.queryTotal(map);
    }

    /**
     *
     * @param moduleMaintainRecord 实体
     * @return
     */
    @Override
    public int save(ModuleMaintainRecordEntity moduleMaintainRecord) {
        moduleMaintainRecord.setId(RandomUtil.randomString(32));
        return moduleMaintainRecordDao.save(moduleMaintainRecord);
    }

    /**
     *
     * @param moduleMaintainRecord 实体
     * @return
     */
    @Override
    public int update(ModuleMaintainRecordEntity moduleMaintainRecord) {
        return moduleMaintainRecordDao.update(moduleMaintainRecord);
    }

    /**
     *
     * @param id
     * @return
     */
    @Override
    public int delete(String id) {
        return moduleMaintainRecordDao.delete(id);
    }

    /**
     *
     * @param ids
     * @return
     */
    @Override
    public int deleteBatch(String[] ids) {
        return moduleMaintainRecordDao.deleteBatch(ids);
    }

    /**
     *
     * @param list
     * @return
     */
    @Override
    public int updateStatus(List<Map<String, Object>> list) {
        int flag = 0;
        if(list.size()>0){
            for(Map<String,Object> map:list){
                String id = map.get("id")+"";
                ModuleMaintainPlanItemEntity moduleMaintainPlanItem = moduleMaintainPlanItemService.queryObject(id);
                if(moduleMaintainPlanItem != null){
                    moduleMaintainPlanItem.setImplementDate(DateUtil.date());
                    moduleMaintainPlanItem.setBmCheckResult(MapUtil.getInt(map,"whf",0));
                    moduleMaintainPlanItem.setAmConfirmResult(MapUtil.getInt(map,"whl",0));
                    moduleMaintainPlanItem.setRemark(MapUtil.getStr(map,"optionRemark"));
                    if(moduleMaintainPlanItemService.update(moduleMaintainPlanItem) > 0){
                        flag++;
                    }
                }
            }
        }
        return flag;
    }

}
